package md.NJavaBase;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.Map;

//Banner实例
public class Banner {
    public Banner( ViewGroup.LayoutParams layoutParams)
    {
        this.m_LayoutParams = layoutParams;
    }


    public boolean show(int channel)
    {
        BannerLoader loader = BannerDispatcher.I().GetActiveBanner();
        if(loader==null) return false;
        View v = loader.getAD().getView();
        if(v==null) return false;

        //移除旧的Banner
        hide();

        //显示Banner
        m_CurrView = v;
        m_CurrLoader = loader;

        String evtName = String.format("ad_banner_%d",channel);
        if(m_OldView==m_CurrView)
        {
            Map<String,Object> param = new HashMap<>();
            param.put("v","old");
            NJavaBase.trackEvent(evtName,param);
            Log.d(Tag, "show old");
        }else{
            Map<String,Object> param = new HashMap<>();
            param.put("v","new");
            NJavaBase.trackEvent(evtName,param);
            Log.d(Tag, "show new");
        }

        m_OldView=m_CurrView;

        Activity activity = NJavaBase.getActivity();
        activity.addContentView(v,m_LayoutParams);
        v.setVisibility(View.VISIBLE);
        v.setBackgroundColor(Color.argb(0,0, 0, 0));

        return true;
    }

    public void hide()
    {
        if(m_CurrView==null) return;

        ViewGroup vg = (ViewGroup) m_CurrView.getParent();
        if(vg!=null) vg.removeView(m_CurrView);

        //设置为无效状态
        m_CurrLoader.SetInvalidated();

        m_CurrLoader = null;
        m_CurrView = null;
    }

    View m_OldView;
    View m_CurrView;//当前显示的Banner视
    BannerLoader m_CurrLoader;
    final ViewGroup.LayoutParams m_LayoutParams;
    static String Tag = "Banner";
}



        /*
        View view = m_ad.getView();
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity= Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
        activity.addContentView(view,layoutParams);
        view.setVisibility(View.GONE);

        ViewGroup vg = (ViewGroup) v.getParent();
        vg.removeView(v);*/

    /*
    void showBanner(BannerLoader loader )
    {
        for(int i=0;i<mLoaderList.size();i++)
        {
            BannerLoader curr = (BannerLoader)mLoaderList.get(i);
            View view = curr.getAD().getView();
            if(view!=null)
            {
                if(curr==loader) Log.d(Tag,"showBanner "+loader.getAD().adName());
                view.setVisibility(curr==loader?View.VISIBLE:View.GONE);
            }
        }
    }


    * */
